package contactbook.service.impl;

import contactbook.dao.AccountDAOInterface;
import contactbook.dao.GroupDAOInterface;
import contactbook.dao.impl.AccountDAO;
import contactbook.dao.impl.GroupDAO;
import contactbook.model.Account;
import contactbook.model.Group;
import contactbook.service.GroupServiceInterface;

public class GroupService implements GroupServiceInterface {

	private GroupDAOInterface groupDAO;
	private AccountDAOInterface accountDAO;
	private Account account;

	public GroupService(Account account) {
		groupDAO = new GroupDAO();
		accountDAO = new AccountDAO();
		this.account = account;
	}

	@Override
	public Group addGroup(Group group) {
		if (account.isLogged()) {
			Group g = groupDAO.addGroup(group);
			accountDAO.affectGroup(account.getId(), g.getId());
			return g;
		} else
			return null;
	}

	@Override
	public void deleteGroup(int groupId) {
		if (account.isLogged()) {
			groupDAO.deleteGroup(groupId);
			accountDAO.deleteGroup(account.getId(), groupId);
		}
	}

	@Override
	public Group getGroupById(int groupId) {
		return (account.isLogged()) ? groupDAO.getGroupById(groupId) : null;
	}

	public boolean exists(int groupId) {
		return (account.isLogged()) ? groupDAO.exists(groupId) : false;
	}

	public boolean exists(Group g) {
		return (account.isLogged()) ? groupDAO.exists(g) : false;
	}
}
